/**
 * Copyright
 */
package com.fare.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;


public class DateUtil {
	private static Logger log = Logger.getLogger(DateUtil.class);
	
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	
	
	public static String getNextDateByDateStr(String datestr,int next) throws Exception{
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		calendar.setTime(sdf.parse(datestr));
		calendar.add(Calendar.DATE,next);
		log.info("datestr:"+datestr+"-->"+sdf.format(calendar.getTime()));
		return sdf.format(calendar.getTime());
	}
	public static String getNextDateByDateStr(String datestr) throws Exception{
		return getNextDateByDateStr(datestr,1);
	}
	
	/**
	 * 
	 * 获取当前时间前一天的开始和结束小时
	 * @param currTime
	 * @return
	 */
	public static Timestamp[] getPreDayStartAndEnd(Timestamp currTime) {
		Calendar c = Calendar.getInstance();
		c.setTime(currTime);
		
		c.set(Calendar.HOUR_OF_DAY, -24);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		Timestamp start = new Timestamp(c.getTimeInMillis());
		
		c.set(Calendar.HOUR_OF_DAY, 24);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		Timestamp end = new Timestamp(c.getTimeInMillis());
		
		return new Timestamp[]{start, end};
	}
	
	
	public static Timestamp getMonthFirDay(Integer year, Integer month) {
		Calendar cal =Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month-1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return new Timestamp(cal.getTimeInMillis());
	}
	
	public static Timestamp getMonthLastDay(Integer year, Integer month) {
		Calendar cal =Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return new Timestamp(cal.getTimeInMillis());
	}
	
	
	

}
